package com.ruoyi.business.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.business.domain.TrainMan;
import com.ruoyi.business.domain.vo.CourseVo;
import com.ruoyi.business.mapper.TrainManMapper;
import com.ruoyi.business.utils.ConstPools;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysOss;
import com.ruoyi.system.domain.vo.InnerObjVo;
import com.ruoyi.system.mapper.SysOssMapper;
import com.ruoyi.system.service.ISysRoleService;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.CourseMapper;
import com.ruoyi.business.domain.Course;
import com.ruoyi.business.service.ICourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * courseService业务层处理
 * 
 * @author 吴倩芸
 * @date 2023-03-04
 */
@SuppressWarnings("ALL")
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService
{
    @Autowired
    private CourseMapper courseMapper;

    @Autowired
    private SysOssMapper sysOssMapper;

    @Autowired
    private TrainManMapper trainManMapper;

    @Autowired
    private ISysRoleService sysRoleService;

    /**
     * 查询course
     * 
     * @param id course主键
     * @return course
     */
    @Override
    public CourseVo selectCourseById(Long id)
    {
        Course course = courseMapper.selectCourseById(id);
        CourseVo vo = new CourseVo();
        BeanUtils.copyProperties(course, vo);
        return vo;
    }

    /**
     * 查询course列表
     * 
     * @param course course
     * @return course
     */
    @Override
    public List<CourseVo> selectCourseList(Course course)
    {
        // 判断当前用户是否为超级管理员
        Long userId = SecurityUtils.getUserId();
        List<Long> roles = sysRoleService.selectRoleListByUserId(userId);
        List<Course> courses = null;
        if (isRole(roles, ConstPools.ADMIN_ROLE_ID) || isRole(roles, ConstPools.SUB_ADMIN_ROLE_ID)) {
            courses = courseMapper.selectCourseList(course);

        }else if (isRole(roles, ConstPools.TRAIN_MAN_ROLE_ID)) {
            course.setTrainManId(userId);
            courses = courseMapper.selectCourseList(course);
        }else {
            return null;
        }
        return courses.stream().map(item -> {
            CourseVo vo = new CourseVo();
            BeanUtils.copyProperties(item, vo);
            SysOss sysOss = sysOssMapper.selectById(item.getCourseImage());
            vo.setCourseImageUrl(sysOss.getUrl());
            // 查询教练信息
            if (null != item.getTrainManId()) {
                TrainMan trainMan = trainManMapper.selectTrainManById(item.getTrainManId());
                vo.setTrainMan(trainMan);
            }
            return vo;
        }).collect(Collectors.toList());
    }

    /**
     * 新增course
     * 
     * @param course course
     * @return 结果
     */
    @Override
    public int insertCourse(Course course)
    {
        course.setCreateTime(DateUtils.getNowDate());
        if (null == course.getTrainManId()) {
            course.setTrainManId(SecurityUtils.getUserId());
        }
        return courseMapper.insertCourse(course);
    }

    /**
     * 修改course
     * 
     * @param course course
     * @return 结果
     */
    @Override
    public int updateCourse(Course course)
    {
        course.setUpdateTime(DateUtils.getNowDate());
        return courseMapper.updateCourse(course);
    }

    /**
     * 批量删除course
     * 
     * @param ids 需要删除的course主键
     * @return 结果
     */
    @Override
    public int deleteCourseByIds(Long[] ids)
    {
        return courseMapper.deleteCourseByIds(ids);
    }

    /**
     * 删除course信息
     * 
     * @param id course主键
     * @return 结果
     */
    @Override
    public int deleteCourseById(Long id)
    {
        return courseMapper.deleteCourseById(id);
    }

    @Override
    public List<InnerObjVo> queryRefCourseInnerObj(String name) {
        // 如果判断当前调用的人是否为教练
        Long userId = SecurityUtils.getUserId();
        List<Long> roles = sysRoleService.selectRoleListByUserId(userId);
        if (isRole(roles, ConstPools.ADMIN_ROLE_ID)) {
            // 如果当前的用户是超级管理员，就可以查询所有信息
            return courseMapper.queryAllRefCourseInnerObj(name);
        }else if (isRole(roles, ConstPools.TRAIN_MAN_ROLE_ID)) {
            // 如果是教练就只能查询与他有关的课程信息
            return courseMapper.queryTrainManRefCourseInnerObj(name, userId);
        }
        return null;
    }

    @Override
    public List<CourseVo> selectCourseAllList(Course course) {
        // 判断当前用户是否为超级管理员
        List<Course> courses = courseMapper.selectCourseList(course);
        return courses.stream().map(item -> {
            CourseVo vo = new CourseVo();
            BeanUtils.copyProperties(item, vo);
            SysOss sysOss = sysOssMapper.selectById(item.getCourseImage());
            vo.setCourseImageUrl(sysOss.getUrl());
            // 查询教练信息
            if (null != item.getTrainManId()) {
                TrainMan trainMan = trainManMapper.selectTrainManById(item.getTrainManId());
                vo.setTrainMan(trainMan);
            }
            return vo;
        }).collect(Collectors.toList());
    }

    private Boolean isRole(List<Long> roles, Long roleId) {
        for (Long role : roles) {
            if (role == roleId) {
                return true;
            }
        }
        return false;
    }



}
